"use strict";
/* Controllers */
app.controller("ctiListController", ['$scope', '$location', '$rootScope', '$routeParams', 'ctiListService', function ($scope, $location, $rootScope, $routeParams, ctiListService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};
    //vm.p.appID = $routeParams.appID; //  获取应用模块的名称

    //datetimePicker('datetimepicker','myDate');
    //if($rootScope.ds==null) {
    //    vm.ds = getDateStr(-1);
    //    $rootScope.ds = getDateStr(-1);
    //}else {
    //    vm.ds = $rootScope.ds;
    //}

    vm.dim = 'week';
    vm.dims = {};
    vm.dims.items = [{ idx: 0, label: '日账单', id: 'day' }, { idx: 1, label: '周账单', id: 'week' }];

    if ($rootScope.dim == null) {
        vm.dim = vm.dims.items[0];
        $rootScope.dim = vm.dim;
    } else {
        vm.dim = vm.dims.items[$rootScope.dim.idx];
    }

    $scope.vm.onDimSelected = function (id) {
        $scope.initializeController();
        $rootScope.dim = $scope.vm.dim;
    };

    var errorFn = function (result) {
        alert(result);
    };

    $scope.listCti = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        var params = {};
        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        ctiListService.listCti(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
            pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $resultTable.bootstrapTable('hideColumn', 'biz');
        $resultTable.bootstrapTable('hideColumn', 'app');

        $scope.listCti();

    };

    $scope.initializeController();

}]);


/* Services */
app.service('ctiListService', ['ajaxService', function (ajaxService) {

    this.listCti = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/onduty-app-service/v1/cti/listCti.do", successFunction, errorFunction);
    };
}]);

function ctiIDOptFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="#/star/onduty/cti_add/id/', row.ctiId, '" target="_blank">', "<u>修改</u>", '</a>'
    ].join('');
}

function ctiNmFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return row.ctiName + '<br/>(' + row.ctiId + ')'
}

function resolvegroupIdsFmt(value, row, index) {
    if (value == null) {
        return "-";
    }

    var result = '';
    var items = value.split(',');
    for(var i=0;i<items.length;++i) {
        var kv = items[i].split(':');
        var item= ['<a href="#/star/onduty/resolvegroup_add/code/',kv[1],'" target="_blank">',kv[0],'</a>','(',kv[1],')'].join('');
        if(i>0) {
            result += '<br/>';
        }
        result += item;
    }
    return result;
}


function ctiTypeFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    switch (value) {
        case 'data':
            return "数据";//"新建";
        case 'business':
            return "业务";//"修改";
        case 'devops':
            return "运维";//"删除";
        default:
            return value;
    }
}
